<template>
  <el-form ref="deptForm" :model="form" :rules="rules" label-width="120px">
    <el-form-item label="部门名称" prop="name">
      <el-input v-model="form.name" style="width:80%" placeholder="1-50个字符" />
    </el-form-item>
    <el-form-item label="部门编码" prop="code">
      <el-input v-model="form.code" style="width:80%" placeholder="1-50个字符" />
    </el-form-item>
    <el-form-item label="部门负责人" prop="manager">
      <el-select v-model="form.manager" style="width:80%" placeholder="请选择">
        <el-option v-for="item in employees" :key="item.id" :label="item.username" :value="item.username" />
      </el-select>
    </el-form-item>
    <el-form-item label="部门介绍" prop="introduce">
      <el-input v-model.number="form.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="hSubmit">确定</el-button>
      <el-button @click="hCancel">取消</el-button>
    </el-form-item>
  </el-form>
</template>

<script>

import { getEmployeeSimple } from '@/api/employees'
import { adddepartment, getdepartmentById, updatedepartmentById } from '@/api/departments'
export default {
  props: {
    id: {
      type: String,
      required: true
    },
    isedit: {
      type: Boolean,
      required: true
    },
    originlist: {
      type: Array,
      required: true
    }
  },

  data() {
    const validateCode = (rule, value, callback) => {
      let codeList = null
      if (this.isedit) {
        // 编辑过滤出(除正在编辑的部门之外)的所有部门
        codeList = this.originlist.filter(item => item.id !== this.id).map(({ code }) => code)
      } else {
        codeList = this.originlist.map(({ code }) => code)
      }
      // 校验value是否在originList数组中
      if (codeList.includes(value)) {
        callback(new Error(value + '已存在'))
      } else {
        callback()
      }
    }
    const validateName = (rule, value, callback) => {
      // 添加时不允许和子部门重复
      // 编辑时不允许和兄弟部门名称重复
      let nameList = null
      if (this.isEdit) {
        // 根据当前点击部门id找到父部门pid
        const pid = this.originlist.find(item => item.id === this.id).pid
        // 根据pid过滤出来的所有子部门，就是兄弟部门
        nameList = this.originlist.filter(item => item.pid === pid && item.id !== this.id).map(({ name }) => name)
      } else {
        nameList = this.originlist.filter(item => item.pid === this.id).map(({ name }) => name)
      }
      if (nameList.includes(value)) {
        callback(new Error(value + '已存在'))
      } else {
        callback()
      }
    }
    return {
      form: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      rules: {
        name: [
          { required: true, message: '请输入部门名称', trigger: 'blur' },
          { min: 2, max: 10, message: '请输入2~10个字符', trigger: 'blur' },
          { validator: validateName, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入部门编码', trigger: 'blur' },
          { min: 2, max: 10, message: '请输入2~10个字符', trigger: 'blur' },
          { validator: validateCode, trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '选择部门管理者', trigger: 'change' }
        ],
        introduce: [
          { required: true, message: '请输入部门介绍', trigger: 'blur' },
          { min: 2, max: 300, message: '请输入2~300个字符', trigger: 'blur' }
        ]
      },
      employees: []
    }
  },

  created() {
    // 页面加载发送下拉菜单
    this.loadEmployeeSimple()
    // 页面加载发送编辑页面
    this.loadDetail()
  },

  methods: {
    // 发送编辑请求渲染页面
    async  loadDetail() {
      try {
        if (!this.isedit) return
        const res = await getdepartmentById(this.id)
        // console.log(res.data)
        this.form = res.data
      } catch (e) {
        console.log(e)
      }
    },
    // 发请请求渲染下拉菜单数据
    async  loadEmployeeSimple() {
      try {
        const res = await getEmployeeSimple()
        console.log('111', res)
        this.employees = res.data
      } catch (e) {
        console.log(e)
      }
    },
    hSubmit() {
      this.$refs.deptForm.validate(valid => {
        if (!valid) return
        if (this.isedit) {
          // 编辑操作
          this.doEdit()
        } else {
          // 添加操作
          this.doAdd()
        }
      })
    },
    async  doAdd() {
      try {
        const res = await adddepartment({ ...this.form, pid: this.id })
        this.$message.success(res.message)
        console.log('222', res)
        this.$emit('success')
      } catch (e) {
        this.$message.error(e.message)
      }
    },
    async  doEdit() {
      try {
        const res = await updatedepartmentById(this.form)
        this.$message.success(res.message)
        console.log('这是修改数据', res)
        this.$emit('success')
      } catch (e) {
        this.$message.error(e.message)
      }
    },
    hCancel() {
      // 移除表单校验
      // this.$refs.deptForm.resetFields()
      this.$emit('clear')
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
